﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace QLK.Database.DALs
{
    public class ImportInvoiceDAL
    {
        private ManageStoreDataContext ctx;
        public ImportInvoiceDAL()
        {
            ctx = new ManageStoreDataContext();
        }
        public ImportInvoice GetImportInvoiceByImportID(int importID)
        {
            ImportInvoice im = ctx.ImportInvoices.Single<ImportInvoice>(import => import.ImportID == importID);
            return im;
        }
        public IEnumerable<ImportInvoice> GetAllImportInvoice()
        {
            IEnumerable<ImportInvoice> source = from import in ctx.ImportInvoices
                                                orderby import.InvoceID
                                           select import;
            return source;
        }
        public bool DeleteImportInvoiceByImportID(int importID)
        {
            ImportInvoice import = ctx.ImportInvoices.Single<ImportInvoice>(im =>im.ImportID == importID);
            if (import == null)
                return false;
            ctx.ImportInvoices.DeleteOnSubmit(import);
            try
            {
                ctx.SubmitChanges();
                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return false;
            }
        }
        public bool CheckDuplicateImportInvoiceByInvoiceID(string invoiceID)
        {
            ImportInvoice import = ctx.ImportInvoices.SingleOrDefault<ImportInvoice>(im =>im.InvoceID.Equals(invoiceID));
            if (import == null)
                return false;
            return true;
        }
        public bool UpdateImportInvoice(ImportInvoice import)
        {
            ImportInvoice curr = ctx.ImportInvoices.Single<ImportInvoice>(im => im.ImportID ==import.ImportID);

            if (curr == null)
                return false;
            curr.InvoceID = import.InvoceID;
            curr.StorageID = import.StorageID;
            curr.SupplierID = import.SupplierID;
            curr.EmployeeID = import.EmployeeID;
            curr.TotalValues = import.TotalValues;
            try
            {
                ctx.SubmitChanges();
                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return false;
            }
        }
        public bool InsertImportInvoice(ImportInvoice import)
        {
            try
            {
                ctx.ImportInvoices.InsertOnSubmit(import);
                ctx.SubmitChanges();
                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return false;
            }
        }
        //==
        //public Supplier SearchSupplierGetAll(int supplierID)
        //{
        //    Supplier supp = ctx.Suppliers.Single<Supplier>(suppli => suppli.SupplierID == supplierID);
        //    return supp;
        //}

        ////public Employee SearchEmployeeGetAll(int employeeID)
        ////{
        ////    Employee em = ctx.Employees.Single<Employee>(emp => emp.EmployeeID == employeeID);
        ////        return em;
        ////}

        public IEnumerable<ImportInvoice> GetImportInvoiceAdvanced(string invoiceName, string storeName, string supplierName, string employeeName)
        {
            string where = string.Empty;
            string connector = "AND";
            if (invoiceName != string.Empty)
                where += string.Format("{0} = {1} AND", Columns.InvoiceID, invoiceName);
            if (storeName != string.Empty)
                where += string.Format("{0}= LIKE '%{1}%' AND", Columns.StorageName, storeName);
            if (supplierName != string.Empty)
                where += string.Format("{0} LIKE '%{1}%' AND", Columns.SupplierName, supplierName);
            if (employeeName != string.Empty)
                where += string.Format("({0} LIKE '%{1}%' OR {2} LIKE '%{1}%')", Columns.LastName, employeeName, Columns.FirstName);
            if (where != string.Empty)
                where = "WHERE " + where.TrimEnd(connector.ToCharArray());
            return null;
        }
    }
}
